/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2022-09-26
 * Time: 12:07
 */

class Node {
    public int data;
    public Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

// 1、无头单向非循环链表实现
public class SingleLinkedList {

    private int size = 0;

    public Node head = null; // 头节点

    //头插法
    public void addFirst(int data) {
        Node newNode = new Node(data);

        if (head != null) {
            newNode.next = head;
        }
        head = newNode;
        size++;
    }
    //尾插法
    public void addLast(int data) {
        Node newNode = new Node(data);
        if(head == null) {
            head = newNode;
        } else {
            Node tail = head;
            while (tail.next != null) {
                tail = tail.next;
            }
            tail.next = newNode;
        }
        size++;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(index < 0 || index > size) {
            throw new RuntimeException("index位置不合法");
        }
        if(index == 0) {
            addFirst(data);
        } else if(index == size) {
            addLast(data);
        } else {
            Node cur = head;
            while (cur != null) {
                if(index == 1) {
                    Node newNode = new Node(data);

                    Node curNext = cur.next;
                    cur.next = newNode;
                    newNode.next = curNext;
                    break;
                }
                cur = cur.next;
                index--;
            }
        }
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node pos = head;
        while(pos != null) {
            if(pos.data == key) {
                return true;
            }
            pos = pos.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }

        if(head.data == key) { //头删
            Node pos = head;
            head = head.next;
            pos.next = null;
            size--;

            return;
        }

        Node pos = head;
        Node prevPos = head; // pos节点的前驱

        while(pos != null) {
            if(pos.data == key) {
                prevPos.next = pos.next;
                pos.next = null;

                size--;
                return;
            }
            prevPos = pos;
            pos = pos.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) { return; }

        while (head.data == key) { //头删
            Node pos = head;
            head = head.next;
            pos.next = null;
            size--;

            if(head == null) { return; }
        }

        Node pos = head;
        Node prevPos = head;
        while(pos != null) {
            if(pos.data == key) {
                prevPos.next = pos.next;
                size--;
                pos = pos.next;
                continue;
            }
            prevPos = pos;
            pos = pos.next;
        }

    }
    //得到单链表的长度
    public int size() {
        return size;
    }
    // 打印
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    // 清空链表
    public void clear() {
        head = null;
    }
}
